package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dao.OrderFoodDao;
import entiey.OrderFood;
import util.JDBCUtil;
import util.ResultSetCreateObject;

public class OrderFoodDaoImpl implements OrderFoodDao,ResultSetCreateObject<OrderFood>{

	@Override
	public int add(OrderFood t) {
		int num=JDBCUtil.executeUpdate("insert into orderfood (orderNo,wid,tid,mid,foodName,foodPrice,foodNum,orderTime,remark) values(?,?,?,?,?,?,?,now(),?)", t.getOrderNo(),t.getWid(),t.getTid(),t.getMid(),t.getFoodName(),t.getFoodPrice(),t.getFoodNum(),t.getRemark());
		return  num;
	}

	@Override
	public int delete(int no) {
		int num=JDBCUtil.executeUpdate("delete from orderfood where id=?", no);
		return num;
	}
	@Override
	public int deleteByOrderNo(String orderNo) {
		int num = JDBCUtil.executeUpdate("delete from orderfood where orderNo=? ", orderNo);
		return num;
	}
	@Override
	public int update(OrderFood t) {
		int num=JDBCUtil.executeUpdate("update orderfood set(wid=?,tid=?,mid=?,foodName=?,foodPrice=?,foodNum=?,orderTime=?,remark=?) where orderNo=?",t.getWid(),t.getTid(),t.getMid(),t.getFoodName(),t.getFoodPrice(),t.getFoodNum(),t.getRemark(),t.getOrderNo());
		return num;
	}

	@Override
	public List<OrderFood> findAll() {
		List<OrderFood> list = JDBCUtil.executeQuery("select * from orderfood",this);
		return list;
	}
	@Override
	public OrderFood findById(int id) {
		List<OrderFood> list = JDBCUtil.executeQuery("select * from orderfood where id=?",this,id);
		OrderFood food=null;
		if(list.size()>0) {
			food=list.get(0);
		}
		return food;
	}
	@Override
	public List<OrderFood> findByOrderNo(String orderNo) {
		List<OrderFood> list = JDBCUtil.executeQuery("select * from orderfood where orderNo=?",this,orderNo);
		return list;
	}

	@Override
	public OrderFood create(ResultSet rs) {
		OrderFood food=new OrderFood();
		try {
			food.setOrderNo(rs.getString("orderNo"));
			food.setWid(rs.getInt("wid"));
			food.setTid(rs.getInt("tid"));
			food.setMid(rs.getInt("mid"));
			food.setFoodName(rs.getString("foodName"));
			food.setFoodPrice(rs.getDouble("foodPrice"));
			food.setFoodNum(rs.getInt("foodNum"));
			food.setOrderTime(rs.getTimestamp("orderTime"));
			food.setRemark(rs.getString("remark"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return food;
	}

	@Override
	public Map<String, Integer> getOrderCount() {
		Map<String,Integer> map = new HashMap<String, Integer>();
		String sql="select orderNo,count(1) from orderfood GROUP BY orderNo";
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				map.put(rs.getString("orderNo"), rs.getInt("count(1)"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(rs!=null) {
					rs.close();
					rs = null;
				}
				if(ps!=null) {
					ps.close();
					ps = null;
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return map;
	}

}
